package com.rambo.androidnews.mvp;

import com.haibin.retrofit.Response;
import com.haibin.retrofit.Retrofit;
import com.haibin.retrofit.call.Callback;
import com.rambo.androidnews.mvp.repository.WanAdRepository;
import com.rambo.baselib.bean.LoadDataConfig;
import com.rambo.baselib.httpapi.WanAdApiService;
import com.rambo.baselib.model.wanad.WanAdApiArticle;
import com.rambo.baselib.model.wanad.WanAdApiPage;
import com.rambo.baselib.model.wanad.WanAdApiResult;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.util.LogUtil;
import com.rambo.baselib.util.ToastUtil;

import java.util.List;

/***********************************************************************
 * <p>@description:
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/14 10:39
 **********************************************************************/
public class WanAdPresenter extends BasePresenter<IWanAdView> {
    public static final String API_HOME_ARTICLE = "api_home_article";
    public static final String API_WX_ARTICLE = "api_wx_article";
    public static final String API_PROJECT = "api_project";
    public static final String API_PROJECT_CASE = "api_project_case";
    private WanAdRepository repository;

    public WanAdPresenter() {
        repository = new WanAdRepository();
    }

    public void getHomeArticle(LoadDataConfig loadDataConfig) {
//        LogUtil.e("=========getHomeArticle====");
        repository.getHomeArticle(new Callback<WanAdApiResult<WanAdApiPage<List<WanAdApiArticle>>>>() {
            @Override
            public void onResponse(Response<WanAdApiResult<WanAdApiPage<List<WanAdApiArticle>>>> response) {
                LogUtil.e("============="+response.getBodyString());
                if (isAttach()) {
                    getView().loadDataSuccess(response.getBody(), API_HOME_ARTICLE);
                    if (loadDataConfig.isLoadingMore && response.getBody() != null) {
                        loadDataConfig.curPage++;
                    }
                }
            }

            @Override
            public void onFailure(Exception e) {
                if (isAttach()) {
                    ToastUtil.toast(e.getMessage());
                    getView().loadDataFailure();
                }
            }
        }, loadDataConfig.curPage--, loadDataConfig.pageSize);
    }

    public void getWxArticle(LoadDataConfig loadDataConfig) {
//        LogUtil.e("=========getWxArticle====");
        repository.getWxArticle(new Callback<WanAdApiResult<WanAdApiPage<List<WanAdApiArticle>>>>() {
            @Override
            public void onResponse(Response<WanAdApiResult<WanAdApiPage<List<WanAdApiArticle>>>> response) {
//                LogUtil.e("============="+response.getBodyString());
                if (isAttach()) {
                    getView().loadDataSuccess(response.getBody(), API_WX_ARTICLE);
                    if (loadDataConfig.isLoadingMore && response.getBody() != null) {
                        loadDataConfig.curPage++;
                    }
                }
            }

            @Override
            public void onFailure(Exception e) {
                if (isAttach()) {
                    ToastUtil.toast(e.getMessage());
                    getView().loadDataFailure();
                }
            }
        }, loadDataConfig.curPage--, loadDataConfig.pageSize);
    }

    public void getProject(LoadDataConfig loadDataConfig, int cid) {
//        LogUtil.e("=========getProject====");
        repository.getProject(new Callback<WanAdApiResult<WanAdApiPage<List<WanAdApiArticle>>>>() {
            @Override
            public void onResponse(Response<WanAdApiResult<WanAdApiPage<List<WanAdApiArticle>>>> response) {
//                LogUtil.e("============="+response.getBodyString());
                if (isAttach()) {
                    getView().loadDataSuccess(response.getBody(), API_PROJECT);
                    if (loadDataConfig.isLoadingMore && response.getBody() != null) {
                        loadDataConfig.curPage++;
                    }
                }
            }

            @Override
            public void onFailure(Exception e) {
                if (isAttach()) {
                    ToastUtil.toast(e.getMessage());
                    getView().loadDataFailure();
                }
            }
        }, loadDataConfig.curPage--, loadDataConfig.pageSize, cid);
    }

    public void getProjectCase() {
        repository.getProjectCase(new Callback<WanAdApiResult<List<WanAdApiArticle>>>() {
            @Override
            public void onResponse(Response<WanAdApiResult<List<WanAdApiArticle>>> response) {
                if (isAttach()) {
                    getView().loadDataSuccess(response.getBody());
                }
            }

            @Override
            public void onFailure(Exception e) {
                if (isAttach()) {
                    ToastUtil.toast(e.getMessage());
                    getView().loadDataFailure();
                }
            }
        });
    }
}
